import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/common_model/notice_label_list.dart';
import 'package:yysk_app/model/common_model/notice_list_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/api/common_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/utils/js_tool.dart';


class AnnouncementController extends GetxController {
  final currentTab = ''.obs;
  final platformList = RxList<dynamic>([{},{},{},{},{},{},{}]);
  final newsList = RxList<dynamic>();
  //公告
   final noticeList = RxList<NoticeListItem>();
  final noticeListLabel = RxList<NoticeLabelItem>();
   int pageNum = 1;
  final loading = false.obs;

  int platformPage = 1;
  int newsPage = 1;
  final ScrollController scrollController = ScrollController();

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(() {
      if (scrollController.position.pixels == scrollController.position.maxScrollExtent) {
        getNoticeList(loadMore: true);
        // if (currentTab.value == 'platform') {
        //   // getPlatformList(loadMore: true);
        // }
        // if (currentTab.value == 'news') {
        //   // getNewsList(loadMore: true);
        // }
      }
    });
    getNoticeListlabel().then((res)=>{
        getNoticeList(),
        
  
    });
    // getPlatformList();
    // getNewsList();
  }

  Future<void> handleRefresh() async {
           getNoticeList(loadMore: false);
  
    return;
  }
// 获取 notice公告数据label
   Future<void> getNoticeListlabel()async {
    await http.netFetch<List<NoticeLabelItem>>(
      CommonApi.noticeLabel,
      method: Method.post,
      success: (result) {
        noticeListLabel.value = result.data!  ;
            currentTab.value = '${noticeListLabel[0].id!}';
      },
    );
  }
    //获取notice公告数据
  Future<void> getNoticeList({loadMore = false}) {
    loading.value = true;

    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      return http.netFetch<NoticeListModel>(
        CommonApi.noticeList,
        method: Method.post,
        params: {'pageNum': pageNum, 'pageSize': 10,"noticeLabelId": currentTab.value},
        success: (result) {
          List<NoticeListItem> items = result.data?.list ?? [];
          loading.value = false;
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            noticeList.clear();
          }
          noticeList.addAll(items);
        },
      );
    } catch (e) {
      debugPrint(e.toString());
      loading.value = false;
      if (loadMore) {
        pageNum--;
      }
      return Future.error(e);
    }
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  void changeTab(String val) {
       currentTab.value = val;
       pageNum = 1;
       getNoticeList();
  }
}
